from flask import Flask, request, send_file
import uuid
import my_app_config
from pdf_translator import PDFTranslator


app = Flask(__name__)

language_dict = {
    "zh": "中文",
    "de": "德语",
    "fr": "法语",
    "ko": "韩语",
    "ja": "日语"
}

@app.route('/api/upload', methods=['POST'])
def upload_file():
    file = request.files['file']  # 'file'为前端表单中文件输入字段的名称
    filename = str(uuid.uuid4()) + ".pdf"
    tmp_filename = my_app_config.my_app_config.upload_dir + filename
    if file:
        file.save(tmp_filename)  # 保存文件到指定目录
        return filename
    else:
        return "没有上传文件"


@app.route('/api/download/<filename>')
def download_file(filename):
    full_filename = my_app_config.my_app_config.upload_dir + filename
    return send_file(full_filename)

@app.route('/api/translate/<filename>/<language>')
def translate_file(filename, language):
    full_filename = my_app_config.my_app_config.upload_dir + filename
    translate_file = PDFTranslator().translate(src=full_filename, target_language=language_dict[language])
    return translate_file

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8888)